package com.tengju.settlement.application.shared;

import java.util.Optional;

/**
 * @author wuziyang
 */
public class ApplicationException extends RuntimeException {

    private static final long serialVersionUID = 6667775979743208977L;

    private final ApplicationExceptionCode code;

    private final String message;

    private ApplicationException(ApplicationExceptionCode code, Object... param) {
        super(code.getMessage(param));
        message = code.getMessage(param);
        this.code = code;
    }


    private ApplicationException(ApplicationExceptionCode code, Throwable throwable, Object... param) {
        super(code.getMessage(param), throwable);
        message = code.getMessage(param);
        this.code = code;
    }

    public static ApplicationException error(ApplicationExceptionCode code, Object... param) {
        return new ApplicationException(code, param);
    }


    public static ApplicationException error(ApplicationExceptionCode code, Throwable throwable, Object... param) {
        return new ApplicationException(code, throwable, param);
    }

    public ApplicationExceptionCode getCode() {
        return code;
    }
    @Override
    public String getMessage() {
        return Optional.ofNullable(message)
                .orElse(code.name());
    }
}
